package com.yunfeisoft.business.dao.impl;

import com.yunfeisoft.business.dao.inter.BedDao;
import com.yunfeisoft.business.model.Bed;
import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.common.base.BaseDaoImpl;
import com.yunfeisoft.common.sql.builder.SelectBuilder;
import com.yunfeisoft.common.sql.builder.WhereBuilder;
import com.yunfeisoft.common.utils.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ClassName: BedDaoImpl
 * Description: 床位Dao实现
 * Author: Jackie liu
 * Date: 2020-01-13
 */
@Repository
public class BedDaoImpl extends BaseDaoImpl<Bed, String> implements BedDao {

    @Override
    public Page<Bed> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            initPageParam(wb, params);
        }
        return queryPage(wb);
    }

    @Override
    public int deleteByRoomId(String roomId) {
        String sql = String.format("DELETE FROM %s WHERE ROOM_ID_ = ?", domainModel.getTableName());
        return jdbcTemplate.update(sql, roomId);
    }

    @Override
    public List<Bed> queryByRoomId(String roomId) {
        if (StringUtils.isBlank(roomId)) {
            return new ArrayList<>();
        }

        SelectBuilder builder = getSelectBuilder("b");
        builder.column("e.name as elderName")
                .leftJoin(Elder.class).alias("e").on("b.elderId = e.id").build();

        WhereBuilder wb = new WhereBuilder();
        wb.andEquals("roomId", roomId);

        return query(builder.getSql(), wb);
    }

    @Override
    public List<Bed> bedList(String roomId) {
        if (StringUtils.isBlank(roomId)) {
            return new ArrayList<>();
        }
        WhereBuilder wb = new WhereBuilder();
        wb.andEquals("roomId", roomId);
        return query(wb);
    }
}